home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / nextrad.lha / NeXtRad / objIO.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-22  |  5.5 KB  |  184 lines

  1. /* objIO.c */
  2. /* written by : Jason R. Wilson   2/21/93 */
  3.  
  4.  
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include "datastruct.h"
  8.  
  9. void ReadInObject (ObjectCell *Object,char *ObjectFileName)
  10.  
  11. /* Read in an object from .obj file */
  12.  
  13. {
  14.    /*vars --------------------------------------------------*/
  15.  
  16.    FILE *fpObject; /* This is the file pointer for reading in the object */
  17.    FILE *fpMap;
  18.    int loop,loop2;
  19.    int tempvert;
  20.    VertexCell *NewVertex;
  21.    PolygonCell *NewPolygon;
  22.    VertexCell **Lookup;
  23.    PolygonListCell *NewPolygonList;
  24.    VertexListCell *NewVertexList;
  25.    int SurfaceType;
  26.    char MapFileName[20];
  27.    int mapWidth,mapHeight;
  28.    int maxValue;
  29.    unsigned char r,g,b;
  30.    int maploop,maploop2;
  31.    double AverageR,AverageG,AverageB;
  32.    char foo[256];
  33.    int PolygonVertices;
  34.    
  35.    /*code --------------------------------------------------*/
  36.  
  37.    fpObject = fopen(ObjectFileName,"r"); /* open object data file */
  38.    fscanf (fpObject,"%d",&Object->NoofVertices);
  39.    Lookup = (VertexCell **)(malloc(4*(Object->NoofVertices+1)));
  40.    fscanf (fpObject,"%d",&Object->NoofPolygons);
  41.  
  42.  
  43.    /* READ IN VERTICES */
  44.    
  45.    NewVertex = (VertexCell *)(malloc(sizeof(VertexCell)));
  46.    InitVertexCell (NewVertex);
  47.    Object->VertexHead = NewVertex;
  48.    for (loop=1;loop <= Object->NoofVertices;loop++)
  49.    {
  50.       fscanf (fpObject,"%lf %lf %lf",&NewVertex->WorldPosition.x,
  51.           &NewVertex->WorldPosition.y,
  52.           &NewVertex->WorldPosition.z);
  53.       NewVertex->Number = loop;
  54.       Lookup[loop] = NewVertex; /* set up lookup table */
  55.       if (loop == Object->NoofVertices)
  56.      NewVertex->Next = NULL;
  57.       else
  58.       {
  59.      NewVertex->Next = (VertexCell *)(malloc(sizeof(VertexCell)));
  60.      InitVertexCell (NewVertex->Next);
  61.      NewVertex = NewVertex->Next;
  62.       }
  63.    }
  64.    
  65.    /* READ IN POLYGONS */
  66.    
  67.    NewPolygon = (PolygonCell *)(malloc(sizeof(PolygonCell)));
  68.    InitPolygonCell (NewPolygon);
  69.    Object->PolygonHead = NewPolygon;
  70.    for (loop=1;loop <= Object->NoofPolygons;loop++)
  71.    {
  72.       NewVertexList = (VertexListCell *)(malloc(sizeof(VertexListCell)));
  73.       InitVertexListCell (NewVertexList);
  74.       NewPolygon->Culled = false;
  75.       NewPolygon->Vertices = NewVertexList;
  76.       /* Give Polygon Vertices and Give Vertices polygon */
  77.       /* Assume for now only 4 vertices */
  78.       fscanf (fpObject,"%d",&PolygonVertices);
  79.       for (loop2=0;loop2 < PolygonVertices;loop2++)
  80.       {
  81.      fscanf (fpObject,"%d",&tempvert);
  82.      NewVertexList->Vertex = Lookup[tempvert];
  83.      /* Make new polygonlist cell for this vertex */
  84.      NewPolygonList = (PolygonListCell *)(malloc(sizeof(PolygonListCell)));
  85.      InitPolygonListCell (NewPolygonList);
  86.      NewPolygonList->Polygon = NewPolygon;
  87.      /* Insert into polygonlist of vertex */
  88.      if (Lookup[tempvert]->Polygons == NULL)
  89.      {
  90.         Lookup[tempvert]->Polygons = NewPolygonList;
  91.         Lookup[tempvert]->Polygons->Rest = NULL;
  92.      }
  93.      else
  94.      {
  95.         NewPolygonList->Rest = Lookup[tempvert]->Polygons;
  96.         Lookup[tempvert]->Polygons = NewPolygonList;
  97.      }
  98.      if (loop2 == (PolygonVertices - 1))
  99.         NewVertexList->Rest = NULL;
  100.      else
  101.      {
  102.         NewVertexList->Rest =
  103.          (VertexListCell *)(malloc(sizeof(VertexListCell)));
  104.         InitVertexListCell (NewVertexList->Rest);
  105.         NewVertexList = NewVertexList->Rest;
  106.      }
  107.      
  108.       } 
  109.  
  110.       fscanf (fpObject,"%d",&SurfaceType);
  111.       if (SurfaceType == 0)
  112.     {
  113.       NewPolygon->Textured = false;
  114.       fscanf (fpObject,"%lf",&NewPolygon->E.r);
  115.       fscanf (fpObject,"%lf",&NewPolygon->E.g);
  116.       fscanf (fpObject,"%lf",&NewPolygon->E.b);
  117.       
  118.       
  119.       fscanf (fpObject,"%lf",&NewPolygon->R.r);
  120.       fscanf (fpObject,"%lf",&NewPolygon->R.g);
  121.       fscanf (fpObject,"%lf",&NewPolygon->R.b);
  122.     }
  123.       else
  124.     {
  125.       fscanf (fpObject,"%s",MapFileName);
  126.       printf ("reading %s for texture map...\n",MapFileName);
  127.       fpMap = fopen (MapFileName,"r"); /* open texture map data file */
  128.       fscanf (fpMap,"%s",foo);
  129.       fscanf (fpMap,"%d %d",&mapWidth,&mapHeight);
  130.       fscanf (fpMap,"%d",&maxValue);
  131.       NewPolygon->Textured = true;
  132.       /* init. the texture map */
  133.       Object->TextureMap = (Color **)(malloc(mapWidth*sizeof(Color *)));
  134.       for (maploop = 0;maploop < mapWidth;maploop++)
  135.          Object->TextureMap[maploop] = (Color *)(malloc(mapHeight*
  136.                                 sizeof(Color)));
  137.       AverageR = 0.0;
  138.       AverageG = 0.0;
  139.       AverageB = 0.0;
  140.       fscanf (fpMap,"%c",&r); /* temp. hack because of giftoppm */
  141.       for (maploop = 0;maploop < mapHeight;maploop++)
  142.          for (maploop2 = 0;maploop2 < mapWidth;maploop2++)
  143.            {
  144.          fscanf (fpMap,"%c%c%c",&r,&g,&b);
  145.          Object->TextureMap[maploop2][maploop].r = 
  146.            ((double)r/(double)maxValue);
  147.          Object->TextureMap[maploop2][maploop].g = 
  148.            ((double)g/(double)maxValue);
  149.          Object->TextureMap[maploop2][maploop].b = 
  150.            ((double)b/(double)maxValue);
  151.          AverageR += Object->TextureMap[maploop2][maploop].r;
  152.          AverageG += Object->TextureMap[maploop2][maploop].g;
  153.          AverageB += Object->TextureMap[maploop2][maploop].b;
  154.            }
  155.       /* store average ref. Color value in newly read in polygon */
  156.       NewPolygon->E.r = 0.0;
  157.       NewPolygon->E.g = 0.0;
  158.       NewPolygon->E.b = 0.0;
  159.       NewPolygon->R.r = AverageR/(double)(mapHeight*mapWidth);
  160.       NewPolygon->R.g = AverageG/(double)(mapHeight*mapWidth);
  161.       NewPolygon->R.b = AverageB/(double)(mapHeight*mapWidth);
  162.  
  163.       /* store texture map sizes for object */
  164.       Object->mapHeight = mapHeight;
  165.       Object->mapWidth = mapWidth;
  166.       close (fpMap);
  167.     }
  168.  
  169.       if (loop == Object->NoofPolygons)
  170.      NewPolygon->Next = NULL;
  171.       else
  172.       {
  173.      NewPolygon->Next = (PolygonCell *)(malloc(sizeof(PolygonCell)));
  174.      InitPolygonCell (NewPolygon->Next);
  175.      NewPolygon = NewPolygon->Next;
  176.       }
  177.    }
  178.    close (fpObject);
  179.    free (Lookup); /* free up the lookup array */
  180. }
  181.  
  182.  
  183.  
  184.